package com.aliang.redis;

import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class ShardedJedisPoolDemo {

	public static void main(String[] args) {

		//构建连接池配置
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		
		//设置最大连接数
		jedisPoolConfig.setMaxTotal(50);
		
		//设置集群信息
		List<JedisShardInfo> jedisShard = new ArrayList<JedisShardInfo>();
		jedisShard.add(new JedisShardInfo("127.0.0.1",6379));
		//jedisShard.add(new JedisShardInfo("127.0.0.1",6379)); 设置多个
		
		//构建Jedis集群
		ShardedJedisPool shardedJedisPool = new ShardedJedisPool(jedisPoolConfig,jedisShard);
		
		//从连接池中取行jedis分片对象
		ShardedJedis shardedJedis = shardedJedisPool.getResource();
		
		//操作缓存
		shardedJedis.set("aaa", "value_aaa");
		
		String value = shardedJedis.get("aaa");
		
		System.out.println(value);
		
		//关闭连接
		shardedJedis.close();
		
		//关闭连接池
		shardedJedisPool.close();
		
		
		
	}

}
